<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/21
 * Time: 下午12:43
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class AdminController extends AdminBaseController
{
    /**
     * 管理员表
     */
    public function index(){
        $curr = I('get.p/d',0);
        // 获取前端传来的查询词
        $word = I('post.word','');
        $totalNum = 1;
        // 实例化Admin对象
        $Admin = M('admin');
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $sqlWhere = empty($word) ? '1=1' : 'fd_admin.account like "%'.$word.'%" or fd_admin.real_name like "%'.$word.'%" ';
        $list = $Admin->field('fd_admin.id,fd_admin.account,fd_admin.icon,fd_admin.success_num,fd_admin.last_login_time,fd_admin.real_name,fd_admin.status,g.title')->join('left join fd_admin_group ag on fd_admin.id=ag.uid left join fd_group g on ag.group_id = g.id')->where($sqlWhere)->page($curr.','.$totalNum)->select();
        // 赋值数据集
        $this->assign('list',$list);
        // 查询满足要求的总记录数
        $count = $Admin->count();
        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page  = new \Think\Page($count,$totalNum);
        // 分页显示输出
        $show  = $Page->show();
        $this->assign('word',$word);
        // 赋值分页输出
        $this->assign('page',$show);
        $this->assign('title','FD商城管理员表');
        // 输出模板
        $this->display();

        /*$page = I('get.page/d',0);
        $totalNum = 10;
        $startNum = (empty($page) || $page == 1) ? 0 : ($page-1) * $totalNum;//计算开始条
        $adminData = M('Admin')->field('sh_admin.id,sh_admin.account,sh_admin.headurl,sh_admin.succNum,sh_admin.lastLTime,sh_admin.real_name,sh_admin.status,g.title')->join('left join sh_group_admin  ga on sh_admin.id = ga.uid left join sh_group g on ga.group_id = g.id')->limit("".$startNum.",".$totalNum."")->select();
        $this->assign('data',$adminData);
        $this->display();*/
    }

    /**
     * 管理员添加
     */
    public function add(){
        if(IS_GET){
            $group_info = M('group')->getField('id,title');
            $this->assign('group_info',$group_info);
            $this->assign('title','FD商城管理员添加');
            $this->display();
            exit;
        }
        if(IS_AJAX){
            //获取管理员账号
            $account = I('post.account','');
            if(empty($account)){
                $this->ajaxReturn(['state'=>1,'msg'=>'账号不能为空！']);
            }
            //获取管理员角色id
            $group_id = I('post.group_id/d',0);
            if(empty($group_id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'请选择用户组！']);
            }
            //获取密码
            $password = I('post.password','');
            if(empty($password)){
                $this->ajaxReturn(['state'=>1,'msg'=>'请输入密码！']);
            }
            //获取姓名
            $real_name = I('post.real_name','');
            if(empty($real_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'姓名不能为空！']);
            }
            //获取电话
            $tel = I('post.tel','');
            //获取状态
            $status = I('post.status',1);
            //查询条件
            $map = [
                'account' => $account
            ];
            //实例管理员模型
            $Admin = M('Admin');
            $adminData = $Admin->where($map)->find();
            if($adminData){
                $this->ajaxReturn(['state'=>1,'msg'=>'账号已经存在！']);
            }
            $salt = getRandomNumber();  //生成盐
            //开启事务
            $Admin->startTrans();
            $data = [
                'account' => $account,
                'password' => md5($password.$salt),
                'real_name' => $real_name,
                'salt' => $salt,
                'tel' => $tel,
                'status' => $status,
                'add_time' => time()
            ];
            //$isIn = $Admin->execute();
            if($Admin->add($data) == false){
                $Admin->rollback();
            }
            $adminId = $Admin->getLastInsID();
            $isAdminGroup = M('admin_group')->add(['uid'=>$adminId,'group_id'=>$group_id]);
            if($isAdminGroup){
                $Admin->commit();
            }else{
                $Admin->rollback();
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'成功']);
        }
    }

    /**
     * 修改管理员
     */
    public function edit()
    {
        if(IS_GET){
            $id = I('get.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $group_info = M('group')->getField('id,title');
            if($group_info == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'请求失败！']);
            }
            $adminInfo = M('Admin')->field('fd_admin.id,fd_admin.account,fd_admin.real_name,fd_admin.tel,fd_admin.status,ag.group_id')->where(['id' => $id])->join('left join fd_admin_group ag on fd_admin.id=ag.uid')->find();
            $this->assign('adminInfo',$adminInfo);
            $this->assign('group_info',$group_info);
            $this->assign('title','FD商城管理员信息修改');
            $this->display();
        }
        if(IS_POST){
            //超级管理员不能修改
            $id = I('post.id',0);
            if($id == 1 || empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //管理员角色id
            $group_id = I('post.group_id/d',0);
            if(empty($group_id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'请选择用户组！']);
            }
            //获取姓名
            $real_name = I('post.real_name','');
            if(empty($real_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'姓名不能为空！']);
            }
            //获取电话
            $tel = I('post.tel','');
            //实例管理员模型
            $Admin = M('Admin');
            $adminData = $Admin->where(['id'=>$id])->getField('id');
            if(!$adminData){
                $this->ajaxReturn(['state'=>1,'msg'=>'该管理员不存在！']);
            }
            //开启事务
            $Admin->startTrans();
            if($Admin->where(['id'=>$id])->setField(['real_name' => $real_name,'tel' => $tel]) === false || M('admin_group')->where(['uid' => $id])->setField(['group_id' => $group_id]) === false){
                $Admin->rollback();
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败']);
            }
            $Admin->commit();//事务提交
            $this->ajaxReturn(['state'=>0,'msg'=>'成功']);
        }
    }

    /**
     * 删除管理员
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id) || ($id == 1)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            if(M('Admin')->where(array('id'=>$id))->getField('id') == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除假的管理员！']);
            }
            $Admin = M('Admin');
            $Admin->startTrans();
            if($Admin->delete($id) == false || M('admin_group')->where(['uid'=>$id])->delete() == false){
                $Admin->rollback();
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $Admin->commit();
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }

    /**
     * 更变管理员状态
     */
    public function changeState(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $admin_status = M('Admin')->where(['id' => $id])->getField('status');
            if($admin_status === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改状态失败！']);
            }
            if($admin_status){
                M('Admin')->where(['id' => $id])->setField('status',0);
                $this->ajaxReturn(['state'=>0,'msg'=>'已禁止！','data' => 1]);
            }else{
                M('Admin')->where(['id' => $id])->setField('status',1);
                $this->ajaxReturn(['state'=>0,'msg'=>'已开启！','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }
}